package me.snowiest.snowdrift.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Slf4j
@Configuration
public class MybatisPlusConfiguration {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());// 分页插件
        return mybatisPlusInterceptor;
    }

    @Bean
    public MetaObjectHandler metaObjectHandler() {
        return new MetaObjectHandler() {
            @Override
            public void insertFill(MetaObject metaObject) {
                log.info("insert fill starting...");
                this.strictInsertFill(metaObject, "creationTime", LocalDateTime.class, LocalDateTime.now());
                this.strictInsertFill(metaObject, "modificationTime", LocalDateTime::now, LocalDateTime.class);
                this.strictInsertFill(metaObject, "deleted", Integer.class, 0);// 逻辑未删除值(默认为 0) 逻辑已删除值(默认为 1)
                this.strictInsertFill(metaObject, "version", Integer.class, 0);
            }

            @Override
            public void updateFill(MetaObject metaObject) {
                log.info("update fill starting...");
                this.strictInsertFill(metaObject, "modificationTime", LocalDateTime::now, LocalDateTime.class);
            }
        };
    }
}
